/*
 * @Author: 朽木白
 * @Date: 2023-02-06 11:02:58
 * @LastEditors: 1547702880@@qq.com
 * @LastEditTime: 2023-03-04 10:04:46
 * @Description: 用户store
 */
import { defineStore } from 'pinia'
import { getUserInfo, logout,getlimitsOfAuthority } from '@/api'
import type { UserState } from './model/userModel'
import type { UserInfo } from '@/api/user/types'
import { useAuthStore, } from './auth'
import { RESEETSTORE } from '@/utils/reset'
import { LOGIN_URL, ROUTER_WHITE_LIST } from '@/config/config'
import router from '@/router'
export const useUserStore = defineStore({
  id: 'app-user',
  state: (): UserState => ({
    token: '',
    userInfo: null,
    dictList :{},
    menuList : [],
    permissions: [],
    // routerList: [],// 路由
  }),
  actions: {
    // setToken
    setToken(token: string) {
      this.token = token
    },
    // setUserInfo
    setUserInfo(userInfo: UserInfo) {
      this.userInfo = userInfo
    },
    // 重置用户信息
    resetUser() {
      this.token = ''
      this.userInfo = null
    },
    async GetInfoAction() {
    //   const data = { avatar : "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
    //   buttons:  ['cuser.detail', 'cuser.update', 'cuser.delete', 'btn.User.add', 'btn.User.remove', 'btn.User.update', 'btn.User.assgin', 'btn.Role.assgin', 'btn.Role.add', 'btn.Role.update', 'btn.Role.remove', 'btn.Permission.add', 'btn.Permission.update', 'btn.Permission.remove', 'btn.Activity.add', 'btn.Activity.update', 'btn.Activity.rule', 'btn.Coupon.add', 'btn.Coupon.update', 'btn.Coupon.rule', 'btn.OrderList.detail', 'btn.OrderList.Refund', 'btn.UserList.lock', 'btn.Category.add', 'btn.Category.update', 'btn.Category.remove', 'btn.Trademark.add', 'btn.Trademark.update', 'btn.Trademark.remove', 'btn.Attr.add', 'btn.Attr.update', 'btn.Attr.remove', 'btn.Spu.add', 'btn.Spu.addsku', 'btn.Spu.update', 'btn.Spu.skus', 'btn.Spu.delete', 'btn.Sku.updown', 'btn.Sku.update', 'btn.Sku.detail', 'btn.Sku.remove', '123', null, 'BSbbhhhhhhhhhhh', 'aababbabbaaa', '1231231', 'ClientUser', '热热热荣荣', '1', null, ' 而非我', '预约驱蚊器', 'dvd', 'ss', '阿斯顿', '11', '的附属国豆腐干', '3232', 'btn.Attr.FenPei', '4', 'btn.Add1', 'btn.Add2', 'btn.Add3', 'btn.cangku.add'],
    //   name:  "admin",
    //    roles:   ['平台管理员'],
    //   routes :  ['User', 'Role', 'Permission', 'Product', 'Acl',]
    // }
      try {
        const data:any  = await getUserInfo()
        const data1  = await getlimitsOfAuthority()
        const authStore = useAuthStore()
        authStore.routerList= data1.routerList
        // const { avatar, buttons, name, roles, routes } = data
        
        // 存储用户信息
        this.setUserInfo({  name:data.user.name })
        // 存储用户权限信息
        authStore.setAuth({ buttons:[], roles:data.user.roleNames, routes:data.role })
      } catch (error:any) {
        if(error.response.data.code==401){
           this.token= ''
           router.replace(LOGIN_URL)
        }
        // Promise.reject(error)
      }
    },
    async Logout() {
      try {
        await logout()
        RESEETSTORE()
      } catch (error) {
        Promise.reject(error)
      }
    },
  },
  // 设置为true，缓存state
  persist: true,
})
